import React from 'react'
import _ from 'underscore'

import {parseToChart2,restPostApi} from '../apis/core-util'

export default class GeoScope {
  constructor(container) {
    this.container = container;

    this.$state = {
      geoKey:null,
      geoChart:{
        loading:true
      },

      geoSelectedValue:'AVG_RESPONSETIME',
      geoList:[
        {
          label: '平均响应时间',
          value: 'AVG_RESPONSETIME' // 秒
        }, {
          label: '传输数据量',
          value: 'TRANSFER_BYTE' // 整数，字节
        }, {
          label: '网络错误率',
          value: 'NET_ERROR_RATE' // 百分比小数
        }
      ]
    };

    this.geoApi = restPostApi({
      __url: 'http/stat/'+ BlueWare.appId + '/region',
      __type: 'chinaMap',
      viewKey: () =>'chinaMap_'+this.$state.geoSelectedValue,
      appVersionId: ()=>BlueWare.appVersionId,
      indexType:()=>this.$state.geoSelectedValue,
      dateType:()=>this.container.dateType,
      countryId:1
    });
  }

  $geo_list_changing(payload) {
    this.$state.geoSelectedValue = payload.value;

    this.container.emit();
    this.fetch();
  }

  fetch() {
    this.geoApi.fetch().done((data,params)=>{
      this.$state.geoKey = 'chinaMap-'+new Date().getTime();
      this.$state.geoChart = parseToChart2(data,params);

      this.container.emit();
    }).fail((error,params)=>{
      this.$state.geoChart = {error};

      this.container.emit();
    }).progress(()=>{
      this.$state.geoChart = {loading: true};

      this.container.emit();
    });
  }

  $load() {
    this.fetch();
  }
}
